package com.windoent.yugioh.adapter.brvah;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.windoent.yugioh.R;
import com.windoent.yugioh.bean.OnGoingDuiZhenBean;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.widget.RoundImageView;

/**
 * author : robert
 * date   : 2021/5/6 15:26
 * desc   :
 */
public class OnGoingDuiZhenListAdapter extends BaseQuickAdapter<OnGoingDuiZhenBean.DataBean.ResBean, BaseViewHolder> {

    private String role;//选手身份
    private boolean ruishiLun = true;

    public OnGoingDuiZhenListAdapter() {
        super(R.layout.item_list_onging_duijue, null);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, OnGoingDuiZhenBean.DataBean.ResBean item) {
        helper.setText(R.id.tv_game_id, getContext().getString(R.string.string_game_id_colon) + BaseMethod.notNullDefaultValueNull(item.getV_id() + ""))
                .setText(R.id.tv_game_number, BaseMethod.notNullDefaultValueNull(item.getDesk_name()));

        TextView btn = helper.getView(R.id.btn_get_code);
        RelativeLayout parentBg = helper.getView(R.id.rl_background);
        ImageView iv_vs = helper.getView(R.id.iv_vs);
        ImageView tv_jiangbei_left = helper.getView(R.id.tv_jiangbei_left);
        ImageView tv_jiangbei_right = helper.getView(R.id.tv_jiangbei_right);
        LinearLayout ll_jiangbei = helper.getView(R.id.ll_jiangbei);

        RoundImageView iv_avatar_left = helper.getView(R.id.iv_avatar_left);
        TextView tv_name_left = helper.getView(R.id.tv_name_left);
        TextView tv_id_left = helper.getView(R.id.tv_id_left);
        RoundImageView iv_avatar_right = helper.getView(R.id.iv_avatar_right);
        TextView tv_name_right = helper.getView(R.id.tv_name_right);
        TextView tv_id_right = helper.getView(R.id.tv_id_right);
        RelativeLayout rl_right = helper.getView(R.id.rl_right);
        LinearLayout ll_luck_lun_null = helper.getView(R.id.ll_luck_lun_null);
        TextView tv_status = helper.getView(R.id.tv_status);

        parentBg.setBackgroundResource(BaseMethod.notNull(role) && role.equals("player") ? R.drawable.drawable_change_role_item_bg : R.drawable.drawable_change_role_item_gray_bg);
        int status_id = item.getStatus_id();//"1": "进行中", "2": "审核中","3": "成绩有效","4": "成绩无效"
        int a_result = item.getA_result();
        int b_result = item.getB_result();// "0": "还未提交成绩","1": "胜", "2": "平","3": "负","4": "双负"
        boolean is_verify = item.isIs_verify();//true,b_id==0轮空
        vsUiShow(iv_vs, tv_jiangbei_left, tv_jiangbei_right, ll_jiangbei, btn, role, status_id, a_result, b_result, is_verify, item.getB_id());
        infoUiShow(iv_avatar_left, tv_name_left, tv_id_left, iv_avatar_right, tv_name_right, tv_id_right, rl_right, ll_luck_lun_null, item, tv_status);
    }

    //左右pk对象信息
    private void infoUiShow(RoundImageView iv_avatar_left, TextView tv_name_left, TextView tv_id_left, RoundImageView iv_avatar_right, TextView tv_name_right, TextView tv_id_right, RelativeLayout rl_right, LinearLayout ll_luck_lun_null, OnGoingDuiZhenBean.DataBean.ResBean item, TextView tv_status) {
        rl_right.setVisibility(item.isIs_verify() && item.getB_id() == 0 ? View.GONE : View.VISIBLE);
        ll_luck_lun_null.setVisibility(item.isIs_verify() && item.getB_id() == 0 ? View.VISIBLE : View.GONE);
        int status_id = item.getStatus_id();
        int a_result = item.getA_result();
        int b_result = item.getB_result();
        boolean selectedA = status_id == 1 || ((status_id == 2 || status_id == 3) && (a_result == 1 || a_result == 2));
        boolean selectedB = status_id == 1 || ((status_id == 2 || status_id == 3) && (b_result == 1 || b_result == 2));
        SimpleUtils.loadImageForView(getContext(), iv_avatar_left, item.getA_avatar(), R.drawable.img_official_avatar_android);
        SimpleUtils.loadImageForView(getContext(), iv_avatar_right, item.getB_avatar(), R.drawable.img_official_avatar_android);

        iv_avatar_left.setShape(!selectedA);
        iv_avatar_right.setShape(!selectedB);

        tv_name_left.setSelected(selectedA);
        tv_id_left.setSelected(selectedA);
        tv_name_right.setSelected(selectedB);
        tv_id_right.setSelected(selectedB);

        tv_name_left.setText(BaseMethod.notNullDefaultValueNull(item.getA_nickname()));
        tv_id_left.setText(getContext().getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getA_id() + ""));
        tv_name_right.setText(BaseMethod.notNullDefaultValueNull(item.getB_nickname()));
        tv_id_right.setText(getContext().getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getB_id() + ""));
        tv_status.setText(item.isIs_verify() && item.getB_id() == 0 ? getContext().getString(R.string.string_has_ended) : BaseMethod.notNullDefaultValueNull(item.getStatus_view()));
    }

    //pk图片显示
    private void vsUiShow(ImageView iv_vs, ImageView tv_jiangbei_left, ImageView tv_jiangbei_right, LinearLayout ll_jiangbei, TextView btn, String role, int status_id, int a_result, int b_result, boolean is_verify, int b_id) {
        iv_vs.setVisibility(status_id == 3 ? View.GONE : View.VISIBLE);
        ll_jiangbei.setVisibility(status_id != 3 ? View.GONE : View.VISIBLE);
        tv_jiangbei_left.setBackgroundResource((status_id == 2 || status_id == 3) && (a_result == 1 || a_result == 2) ? R.drawable.drawable_jiangbei_sucess_left : R.drawable.drawable_jiangbei_fail_left);
        tv_jiangbei_right.setBackgroundResource((status_id == 2 || status_id == 3) && (b_result == 1 || b_result == 2) ? R.drawable.drawable_jiangbei_sucess_right : R.drawable.drawable_jiangbei_fail_right);
        boolean customerReferee = BaseMethod.notNull(role) && role.equals("customerReferee");
        boolean pReferee = BaseMethod.notNull(role) && role.equals("pReferee");
        btn.setVisibility((customerReferee || pReferee) && status_id == 3 && is_verify && b_id != 0 && ruishiLun ? View.VISIBLE : View.GONE);
    }

    public void setRole(String role) {
        this.role = role;
        notifyDataSetChanged();
    }

    //瑞士轮显示修改
    public void setIsRuiShiLun(boolean ruishiLun) {
        this.ruishiLun = ruishiLun;
        notifyDataSetChanged();
    }
}